package com.dsteiner.xelavos.client.controller;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Date;

import com.dsteiner.widgets.client.IJsUtil;
import com.dsteiner.widgets.client.JsUtil;
import com.dsteiner.widgets.client.Widgets.ILazyWidget;
import com.dsteiner.widgets.client.Widgets.IWidget;
import com.dsteiner.widgets.client.Widgets.WidgetListenerAdapter;
import com.dsteiner.widgets.client.domain.ModelObject;
import com.dsteiner.widgets.client.model.PrefilledTextBoxModel;
import com.dsteiner.xelavos.client.constants.Constants;
import com.dsteiner.xelavos.client.domain.Service;
import com.dsteiner.xelavos.client.model.CreateObjectModel;
import com.google.appengine.api.datastore.Text;

public class CreateServiceController implements IModelObjectBuilder{

	private PrefilledTextBoxModel descriptionTextBoxModel;
	private PrefilledTextBoxModel nameTextBoxModel;
	private PrefilledTextBoxModel placeTextBoxModel;
	private PrefilledTextBoxModel contactTextBoxModel;
	private CreateObjectModel createObjectModel;
	private PrefilledTextBoxModel typeTextBoxModel;
	private IJsUtil jsUtil;

	public ModelObject create() {
		String name = nameTextBoxModel.isEdited() ? nameTextBoxModel.getText() : null;
		String type = typeTextBoxModel.getText();
		String place = placeTextBoxModel.isEdited() ? placeTextBoxModel.getText() : null;
		String description = descriptionTextBoxModel.getText();
		String contact = contactTextBoxModel.isEdited() ? contactTextBoxModel.getText() : null;
		StringBuffer text = new StringBuffer();
		typeTextBoxModel.setHasError(false);
		descriptionTextBoxModel.setHasError(false);
		contactTextBoxModel.setHasError(false);
		if(!typeTextBoxModel.isEdited()){
			text.append("ingrese el tipo de servicio\n");
			typeTextBoxModel.setHasError(true);
		}
		if(!descriptionTextBoxModel.isEdited()){
			text.append("ingrese una descripcion\n");
			descriptionTextBoxModel.setHasError(true);
		}
		if(!contactTextBoxModel.isEdited()){
			text.append("ingrese un contacto\n");
			contactTextBoxModel.setHasError(true);
		}
		
		if(text.length() > 0){
			jsUtil.trackEvent(Constants.CAT_PUBLISHING, Constants.ACT_MSG_SHOWN, text.toString());
			JsUtil.getInstance().showAlert(text.toString());
			return null;
		}

		Service service = new Service();
		if(createObjectModel.getCreateModelObject() != null)
			service = (Service) createObjectModel.getCreateModelObject();
		service.setName(name);
		service.setPlace(place);
		service.setDescription(description != null ? new Text(description) : null);
		service.setRegisteredDate(new Date());
		service.setContact(contact);
		service.setType(type);
		return service;
	}

	public void registerContactTextBoxModel(PrefilledTextBoxModel contactTextBoxModel) {
		this.contactTextBoxModel = contactTextBoxModel;
	}

	public void registerCreateObjectModel(CreateObjectModel createObjectModel) {
		this.createObjectModel = createObjectModel;
		createObjectModel.addPropertyChangeListener("createModelObject", new PropertyChangeListener(){
			public void propertyChange(PropertyChangeEvent evt) {
				updateUi();
			}
		});
	}
	
	public void registerCreateObjectWidget(ILazyWidget createObjectWidget) {
		createObjectWidget.addWidgetListener(new WidgetListenerAdapter() {
			@Override
			public void onAttach(IWidget widget) {
				updateUi();
			}
		});
	}
	
	public void registerDescriptionTextBoxModel(PrefilledTextBoxModel descriptionTextBoxModel) {
		this.descriptionTextBoxModel = descriptionTextBoxModel;
	}

	public void registerNameTextBoxModel(final PrefilledTextBoxModel nameTextBoxModel) {
		this.nameTextBoxModel = nameTextBoxModel;
	}
	
	public void registerPlaceTextBoxModel(PrefilledTextBoxModel placeTextBoxModel) {
		this.placeTextBoxModel = placeTextBoxModel;
	}

	public void registerTypeTextBoxModel(final PrefilledTextBoxModel typeTextBoxModel) {
		this.typeTextBoxModel = typeTextBoxModel;
		typeTextBoxModel.addPropertyChangeListener("hasFocus", new PropertyChangeListener(){
			public void propertyChange(PropertyChangeEvent evt) {
				if(!typeTextBoxModel.hasFocus())
					updateImageSearch();
			}
		});
	}

	private void updateImageSearch() {
		StringBuffer imageSearch = new StringBuffer("");

		if(typeTextBoxModel.isEdited())
			imageSearch.append(typeTextBoxModel.getText()).append(" ");
		
		createObjectModel.setImageSearch(imageSearch.length() > 0 ? imageSearch.toString() : null);
	}

	private void updateUi() {
		ModelObject object = createObjectModel.getCreateModelObject();
		if(object != null && !(object instanceof Service))
			return;
		Service createModelObject = (Service) createObjectModel.getCreateModelObject();
		descriptionTextBoxModel.setText(createModelObject != null ? createModelObject.getDescription() != null ? createModelObject.getDescription().toString() : null : null);
		nameTextBoxModel.setText(createModelObject != null ? createModelObject.getName() : null);
		placeTextBoxModel.setText(createModelObject != null ? createModelObject.getPlace() : null);
		contactTextBoxModel.setText(createModelObject != null ? createModelObject.getContact() : null);
		typeTextBoxModel.setText(createModelObject != null ? createModelObject.getType() : null);
		updateImageSearch();
	}

	public void registerJsUtil(IJsUtil jsUtil) {
		this.jsUtil = jsUtil;
	}
	
}